package com.aier.classes.action;

import java.io.File;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.classes.entity.ClassPhoto;
import com.aier.classes.entity.ClassPhotoImg;
import com.aier.classes.entity.ClassPhotoType;
import com.aier.classes.service.IClassService;
import com.aier.common.common.Constant;
import com.aier.common.common.ImageUtil;

@Scope("prototype")
@Component("classesAlbumAction")
public class ClassesAlbumAction extends ClassesBaseAction {

    /**
     * 班级 - 相册
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "classService")
    private IClassService classService;

    private int s; // 成功:1 ,失败:0
    private String error_info; // 错误提示信息

    private Long cid; // 班级id

    private List<ClassPhoto> cpList; // 班级相册图片列表
    private ClassPhoto cp; // 班级相册
    private Long cpid;// 图片id

    private File file2;// 照片文件
    private String photoName;// 照片原名
    private String img;// 班级相册图片名
    
    
    private String pname; // 相册名称
    private Long cptid;   // 相册id
    private List<ClassPhotoType> cptList; // (班级相册分类)列表
    private List<ClassPhotoImg> cpiList; // (班级相册图片)列表
    private Long cpiid; // 相册图片id

    @Override
    public String execute() {
        return SUCCESS;
    }

    // 查询相册照片
    // 查询某相册下的图片列表
    public String searchClassPhotoImg() {
        cpList = classService.searchClassPhotoList(cid);
        return SUCCESS;
    }

    // 上传相册图片到服务器
    public String uploadClassPhotoImg() {
        img = ImageUtil.uploadImageAdjustable(file2, classgal_upload_url,
                photoName,Constant.num_200,Constant.num_200,Constant.num_500,Constant.num_500,false);
        return SUCCESS;
    }

    // 删除上传的照片
    public String deleteUploadImg() {
        File fo1 = new File(classgal_upload_url + "big/" + img);
        File fo2 = new File(classgal_upload_url + "origin/" + img);
        File fo3 = new File(classgal_upload_url + "small/" + img);
        if (fo1.exists()) {
            fo1.delete();
        }
        if (fo2.exists()) {
            fo2.delete();
        }
        if (fo3.exists()) {
            fo3.delete();
        }
        s = 1;
        return SUCCESS;
    }

    // 上传图片到相册
    public String addUserPhotoImg() {
        cp = new ClassPhoto();
        cp.setCid(cid);
        cp.setImg(img);
        cpid = classService.insertClassPhoto(cp);
        s = 1;
        return SUCCESS;
    }

    // 删除相册图片
    public String deleteUserPhotoImg() {
        s = classService.deleteClassPhoto(cpid);
        return SUCCESS;
    }
    
    // 新的班级相册
    /**
     * 创建相册
     * @return
     */
    public String addClassPhotoType() {
        ClassPhotoType cpt = new ClassPhotoType();
        cpt.setCid(cid);
        cpt.setPname(pname);
        cpt.setCdate(new Timestamp(new Date().getTime()));
        s = classService.insertClassPhotoType(cpt);
        
        return SUCCESS;
    }
    
    /**
     * 上传图片到相册
     * @return
     */
    public String addClassPhotoImg() {
        ClassPhotoImg cpi = new ClassPhotoImg();
        cpi.setCptid(cptid);
        cpi.setImg(img);
        
        Long cpiid = classService.insertClassPhotoImg(cpi);
        // 更新相册封面
        s = classService.updateClassPhotoImg_face(cpiid);
        return SUCCESS;
    }
    
    /**
     * 班级相册类别查询
     * @return
     */
    public String searchClassImgtype() {
        cptList = classService.searchClassPhotoTypeList(cid);
        return SUCCESS;
    }
    
    /**
     * 查询某相册下的图片列表
     * @return
     */
    public String searchClassPhotoImg_type() {
        cpiList = classService.searchClassPhotoImgList(cptid);
        return SUCCESS;
    }
    
    /**
     * 删除相册图片
     * @return
     */
    public String deleteClassPhotoImg() {
        if (null == cpiid) {
            s = 0;
            return SUCCESS;
        }
        ClassPhotoImg upi = classService.searchClassPhotoImg(cpiid);
        s = classService.deleteClassPhotoImg(cpiid);
        s = classService.updateClassPhotoTypeCover("", upi.getCptid());
        return SUCCESS;
    }
    

    public void setClassService(IClassService classService) {
        this.classService = classService;
    }

    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public void setPhotoName(String photoName) {
        this.photoName = photoName;
    }


    public Long getCpid() {
        return cpid;
    }

    public void setCpid(Long cpid) {
        this.cpid = cpid;
    }

    public List<ClassPhoto> getCpList() {
        return cpList;
    }

    public Long getCid() {
        return cid;
    }

    public void setCid(Long cid) {
        this.cid = cid;
    }

    public void setFile2(File file2) {
        this.file2 = file2;
    }
    
    public void setPname(String pname) {
        this.pname = pname;
    }

    public void setCptid(Long cptid) {
        this.cptid = cptid;
    }

    public List<ClassPhotoType> getCptList() {
        return cptList;
    }

    public List<ClassPhotoImg> getCpiList() {
        return cpiList;
    }

    public void setCpiid(Long cpiid) {
        this.cpiid = cpiid;
    }


}